﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.ShipApplyCommand;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Domain.Entity;

namespace WarehouseManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ShipController : ControllerBase
    {

        private readonly IMediator _mediator;

        public ShipController(IMediator mediator)
        {
            _mediator = mediator;
        }



        /// <summary>
        /// 添加发货申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddShipApply(AddShipApplyDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
               command.AddTime = DateTime.Now;
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new CreateShipCommand { Adddto = command });
                result.Code = 200;
                result.Message = "添加成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }



        /// <summary>
        /// 获取发货申请列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<ShowShipDTO>>> GetShipApplyList([FromQuery]SelectShipCommand command)
        {
            ApiResult<PageDTO<ShowShipDTO>> result = new ApiResult<PageDTO<ShowShipDTO>>();
            try
            {
                result.Data  = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "获取成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 删除发货申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelShip(DelShipCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "删除成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }


        /// <summary>
        /// 反填确认发货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FillShipDTO>> BackFillSHip([FromQuery]FillShipCommand command)
        {
            ApiResult<FillShipDTO> result = new ApiResult<FillShipDTO>();

            try
            {
                 result.Data = await _mediator.Send(command);
                result.Code = 200;
                 result.Message = "反填成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 更新发货申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UptShip(UptShipDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new UptShipCommand { dto = command });
                result.Code = 200;
                result.Message = "更新成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加发货确认
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDeliveryConfirmation(DeliveryConfirmation command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                command.AddTime = DateTime.Now;
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new ADDDeliveryConfirmationCommand { add=command});
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 获取发货确认列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDeliveryConfirmation>> GetDeliveryConfirmation([FromQuery] GetDeliveryConfirmationCommand command)
        {
            ApiResult<PageDeliveryConfirmation> result = new ApiResult<PageDeliveryConfirmation>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }





        /// <summary>
        /// 销售利润统计
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<ShowShipDTO>>> GetShipApplyListCount([FromQuery] ShowCommand command)
        {
            ApiResult<PageDTO<ShowShipDTO>> result = new ApiResult<PageDTO<ShowShipDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "获取成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }













    }
}
